<template>
  <el-config-provider :locale="locale">
    <router-view></router-view>
  </el-config-provider>
</template>

<script>
import { ElConfigProvider } from 'element-plus'

import zhCn from 'element-plus/lib/locale/lang/zh-cn'
import axios from "axios";
import moment from "moment";

axios.defaults.withCredentials=true;

export default {
  name: 'App',
  components: {
    // Index,
    [ElConfigProvider.name]: ElConfigProvider,
  },
  data() {
    return {
      locale: zhCn,
    }
  },
  methods:{
    addInterceptor(){
      let router = this.$router;
      let message = this.$message;
      let method = this.formatForm;
      // 添加响应拦截器
      axios.interceptors.response.use(function (response) {
        // 对响应数据做点什么
        let urls = response.request.responseURL.split("/");
        if(urls[3] === "form" && response.config.method === "get"){
          method(response.data.data);
        }
        return response;
      }, function (error) {
        // 对响应错误做点什么
        if (error.response) {
          console.log(error.response.status)
          switch (error.response.status) {
            case 401:
              router.push("/login");
                  break;
            case 403:
               message.error("权限不足");
          }
        }else{
          message.error("网络异常");
        }
        return Promise.reject(error);
      });
    },
    formatForm(form){
      console.log(form);
      form.project.startDate = moment(form.project.startDate).format("YYYY-MM-DD");
      form.project.endDate = moment(form.project.endDate).format("YYYY-MM-DD");
      form.dates = this.getDates(form.project.startDate,form.project.endDate)
      console.log("format:"+form.dates)
    },
    createGuard(){
      this.$router.beforeEach(async (to) => {
        console.log(to.name);
        console.log(this);
        let res = true;
        if (to.name !== undefined) {
          await axios.get(this.$host+"/auth/"+to.name).catch((error) => {
            res = error.response.status !== 403;
          })
        }
        console.log(res);
        return res;
      })
    }
  },
  created() {
    this.addInterceptor();
    this.createGuard();
  }
}
</script>

<style>
#app {
  font-family: Avenir, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  color: #2c3e50;
}
</style>
